package com.bt.lms.ui.action.school_education.stu_basic.familyMember;

import com.bt.lms.ui.page.school_education.stu_basic.Nation;
import com.bt.lms.ui.page.sys.style.BtnStyle;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 家庭成员的编辑功能
 */
public class FamilyMemberEditAction implements EventHandler {

    private VBox vBox;

    private Integer baseWidth = 1200;

    private static HBox familyHBox;

    private static ToggleGroup toggleGroup;

    private List list;

    public FamilyMemberEditAction(VBox vBox, List list, HBox totalHBox) {

        this.vBox = vBox;
        this.list = list;
        this.familyHBox = totalHBox;
    }

    public ToggleGroup getToggleGroup() {
        return toggleGroup;
    }

    public void setToggleGroup(ToggleGroup toggleGroup) {
        this.toggleGroup = toggleGroup;
    }

    @Override
    public void handle(Event event) {
        HBox btnBox = new HBox();
        btnBox.setPrefWidth(1200);
        btnBox.setAlignment(Pos.CENTER_RIGHT);
        btnBox.setSpacing(10);
        Insets insets1 = new Insets(10, 30, 10, 10);
        btnBox.setPadding(insets1);
        Button saveBtn = new Button("保存");
        Button cancelBtn = new Button("取消");
        btnBox.getChildren().add(saveBtn);
        btnBox.getChildren().add(cancelBtn);
        BtnStyle.saveBtn(saveBtn);
        BtnStyle.cancelBtn(cancelBtn);

        /**
         * 取消按钮匿名内部类
         */
        cancelBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                VBox vBox1 = new VBox();
                ObservableList<Node> children = vBox.getChildren();
                HBox hBox = new HBox();
                hBox.setPrefWidth(baseWidth);
                hBox.setAlignment(Pos.CENTER_RIGHT);
                hBox.setSpacing(10);
                Insets insets1 = new Insets(10, 30, 10, 10);
                hBox.setPadding(insets1);
                Button editBtn = new Button("💻编辑");
                Button deleteBtn = new Button("❌删除");
                hBox.getChildren().add(editBtn);
                hBox.getChildren().add(deleteBtn);
                //按钮样式
                BtnStyle.saveBtn(editBtn);
                BtnStyle.cancelBtn(deleteBtn);
                editBtn.setCursor(Cursor.HAND);
                deleteBtn.setCursor(Cursor.HAND);
                //事件
                FamilyMemberEditAction familyMemberEditAction = new FamilyMemberEditAction(vBox, list,familyHBox);
                editBtn.setOnAction(familyMemberEditAction);

                int size = children.size();
                vBox1.getChildren().addAll(hBox, FamilyMemberEditAction.familyHBox);


                children.set(size - 1, vBox1);


            }
        });

        ObservableList<Node> children = this.vBox.getChildren();


        //验证事件
        EditVerifyAction editVerifyAction = new EditVerifyAction(this.vBox, list,familyHBox);
        saveBtn.setOnAction(editVerifyAction);
        saveBtn.setCursor(Cursor.HAND);
        cancelBtn.setCursor(Cursor.HAND);

        //家庭成员信息的主盒子
        VBox totalHBox = new VBox();
        totalHBox.setPrefWidth(baseWidth);
        totalHBox.setSpacing(10);
        totalHBox.getChildren().add(btnBox);

        HBox one = new HBox();
        one.setSpacing(10);
        Label relationLab = new Label("关系");
        relationLab.setPrefWidth(baseWidth / 8);
        Label relationMes = (Label) this.vBox.lookup("#relationMes");
        String relationMesStr = relationMes.getText().trim();
        TextField relationTF = new TextField(relationMesStr);
        relationTF.setId("relationTF");
        relationTF.setPrefWidth(baseWidth / 6);
        Label nameLab = new Label("成员姓名");
        nameLab.setPrefWidth(baseWidth / 8);
        Label memberNameLabMes = (Label) this.vBox.lookup("#memberNameLabMes");
        String memberNameLabMesStr = memberNameLabMes.getText().trim();
        TextField nameTF = new TextField(memberNameLabMesStr);
        nameTF.setId("nameTF");
        nameTF.setPrefWidth(baseWidth / 6);
        Label birthdayLab = new Label("出生年月");
        birthdayLab.setPrefWidth(baseWidth / 8);
        Label birthDataLabMes = (Label) this.vBox.lookup("#birthDataLabMes");
        String birthDataLabMesStr = birthDataLabMes.getText().trim();
        LocalDate birthday = LocalDate.parse(birthDataLabMesStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        DatePicker birthdayDP = new DatePicker(birthday);
        birthdayDP.setId("birthdayDP");
        birthdayDP.setPrefWidth(baseWidth / 6);
        one.getChildren().addAll(relationLab, relationTF, nameLab, nameTF, birthdayLab, birthdayDP);
        totalHBox.getChildren().add(one);

        HBox two = new HBox();
        two.setSpacing(10);
        Label workUnitLab = new Label("成员工作单位");
        workUnitLab.setPrefWidth(baseWidth / 8);
        Label jobUnitLabMes = (Label) this.vBox.lookup("#jobUnitLabMes");
        String jobUnitLabMesStr = jobUnitLabMes.getText().trim();
        TextField workUnitTF = new TextField(jobUnitLabMesStr);
        workUnitTF.setId("workUnitTF");
        workUnitTF.setPrefWidth(baseWidth / 6);
        Label jobLab = new Label("从业");
        jobLab.setPrefWidth(baseWidth / 8);
        Label practitionersLabMes = (Label) this.vBox.lookup("#practitionersLabMes");
        String practitionersLabMesStr = practitionersLabMes.getText().trim();
        TextField jobTF = new TextField(practitionersLabMesStr);
        jobTF.setId("jobTF");
        jobTF.setPrefWidth(baseWidth / 6);
        Label phoneLab = new Label("电话");
        phoneLab.setPrefWidth(baseWidth / 8);
        Label phoneLabMes = (Label) this.vBox.lookup("#phoneLabMes");
        String phoneLabMesStr = phoneLabMes.getText().trim();
        TextField phoneTF = new TextField(phoneLabMesStr);
        phoneTF.setId("phoneTF");
        phoneTF.setPrefWidth(baseWidth / 6);
        two.getChildren().addAll(workUnitLab, workUnitTF, jobLab, jobTF, phoneLab, phoneTF);
        totalHBox.getChildren().add(two);

        HBox three = new HBox();
        three.setSpacing(10);
        Label nationLab = new Label("民族");
        nationLab.setPrefWidth(baseWidth / 8);
        Label nationMes = (Label) this.vBox.lookup("#nationMes");
        String nationMesStr = nationMes.getText().trim();
        ComboBox nationTF = new ComboBox<>();
        nationTF.setId("nationTF");
        //调用方法
        Nation.nation(nationTF);
        if (nationMesStr.equals("汉族")) {
            nationTF.getSelectionModel().select(0);
        }
        nationTF.setPrefWidth(baseWidth / 6);
        Label nationalityLab = new Label("国籍/地区");
        nationalityLab.setPrefWidth(baseWidth / 8);
        Label nationalityMes = (Label) this.vBox.lookup("#nationalityMes");
        String nationalityMesStr = nationalityMes.getText().trim();
        ComboBox nationalityTF = new ComboBox<>();
        nationalityTF.getItems().addAll("中国大陆", "中国香港", "中国台湾", "中国澳门");
        if (nationalityMesStr.equals("中国大陆")) {
            nationalityTF.getSelectionModel().select(0);
        }
        nationalityTF.setId("nationalityTF");
        nationalityTF.setPrefWidth(baseWidth / 6);
        Label healthConditionLab = new Label("健康状况");
        healthConditionLab.setPrefWidth(baseWidth / 8);
        Label healthConditionLabMes = (Label) this.vBox.lookup("#healthConditionLabMes");
        String healthConditionLabMesStr = healthConditionLabMes.getText().trim();
        TextField healthConditionTF = new TextField(healthConditionLabMesStr);
        healthConditionTF.setId("healthConditionTF");
        healthConditionTF.setPrefWidth(baseWidth / 6);
        three.getChildren().addAll(nationLab, nationTF, nationalityLab, nationalityTF, healthConditionLab, healthConditionTF);
        totalHBox.getChildren().add(three);


        HBox four = new HBox();
        four.setSpacing(10);
        Label dutyLab = new Label("专业技术职务");
        dutyLab.setPrefWidth(baseWidth / 8);
        Label professionalSkillLabMes = (Label) this.vBox.lookup("#professionalSkillLabMes");
        String professionalSkillLabMesStr = professionalSkillLabMes.getText().trim();
        TextField dutyTF = new TextField(professionalSkillLabMesStr);
        dutyTF.setId("dutyTF");
        dutyTF.setPrefWidth(baseWidth / 6);
        Label dutyRankLab = new Label("职务级别");
        dutyRankLab.setPrefWidth(baseWidth / 8);
        Label jobRankLabMes = (Label) this.vBox.lookup("#jobRankLabMes");
        String jobRankLabMesStr = jobRankLabMes.getText().trim();
        TextField dutyRankTF = new TextField(jobRankLabMesStr);
        dutyRankTF.setId("dutyRankTF");
        dutyRankTF.setPrefWidth(baseWidth / 6);
        Label emailLab = new Label("电子邮箱");
        emailLab.setPrefWidth(baseWidth / 8);
        Label emileLabMes = (Label) this.vBox.lookup("#emileLabMes");
        String emileLabMesStr = emileLabMes.getText().trim();
        TextField emailTF = new TextField(emileLabMesStr);
        emailTF.setId("emailTF");
        emailTF.setPrefWidth(baseWidth / 6);
        four.getChildren().addAll(dutyLab, dutyTF, dutyRankLab, dutyRankTF, emailLab, emailTF);
        totalHBox.getChildren().add(four);

        Label isEmergencyContact = new Label("是否紧急联系人");
        isEmergencyContact.setPrefWidth(baseWidth / 8);
        HBox hBox2 = new HBox();
        hBox2.setSpacing(10);
        toggleGroup = new ToggleGroup();
        RadioButton yes = new RadioButton("是");
        yes.setId("yesRDB");
        RadioButton no = new RadioButton("否");
        no.setId("noRDB");
        toggleGroup.getToggles().addAll(yes, no);
        hBox2.getChildren().addAll(isEmergencyContact, yes, no);

        totalHBox.getChildren().add(hBox2);


        children.set(1, totalHBox);


    }
}
